#!/usr/bin/env python
import MC_DK480
## import LIA_7260
from sys import exit
from visa import instrument
from time import sleep
from pylab import plot, show
from numpy import savetxt
from easygui import enterbox
## from math import exp

# Numbers are in hundredths of nanometers
res = 500
beg = 82000
end = 94000

filename = enterbox(msg='enter csv filename', title='choose file', default='spectrum.csv')
wdir = 'C:\\Users\\luke\\Documents\\My Dropbox\\pythons\\quantum_tuning\\photoluminescence\\%s'
## spm = MC_DK480.MC_DK480("COM3")
lia = instrument("COM4",baud_rate=19200, data_bits=7)
## spm.reset()
print lia.ask("ID")
## print spm.serialnumber()
## # setup commands
## spm.sltadj(500)         # adjust both slits to 1500 um
## lia.write('TC15')       # 2 s
## lia.write('sen11')      # 50 fA
## lia.write('star2')      # get Magnitude data
## lia.write('lts0')       # lights off

## # see top for range initialization
## x = range(beg,end,res)
## n = len(x)
## xdata = []
## ydata = []
## data = []
## scale = 100e-15*1e-4
## # run spectrum
## print spm.goto((end-beg)/2)
## for wavelen in x:
##     spm.scan(wavelen)
##     waveMC = spm.wave("min")
##     fwaveMC = float(waveMC) * 1e-2
##     sleep(4)
##     mag = lia.ask('*')
##     fmag = float(mag) * scale
##     mag = lia.ask('mag.')
##     fmag = float(mag)
##     xdata.append(fwaveMC)
##     ydata.append(fmag)
##     data.append([fwaveMC,fmag])
##     print str(waveMC) + ',' + str(mag)
## plot(xdata,ydata,'b+')
## # find maxima
## maxdata = []
## for i in range(2,n-2,1):
##     if ((ydata[i] > ydata[i-1]) and (ydata[i] > ydata[i+1])):
##         if ((ydata[i] > ydata[i-2]) and (ydata[i] > ydata[i+2])):
##             maxdata.append(xdata[i])
## print maxdata
## show()
## savetxt(wdir %filename, data, delimiter = ',', fmt='%1.4e')
## lia.write('lts1')       # lights on